﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    public partial class SingleDictionaryControl : PMFDataControl
    {
        public Guid DataDictId { get; set; }

        public SingleDictionaryControl(Guid typeId)
        {
            InitializeComponent();

            DataDictId = typeId;

            this.Loaded += new RoutedEventHandler(DictionaryDataDropList_Loaded);
        }

        void DictionaryDataDropList_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDatas();
        }

        private void LoadDatas()
        {
            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetDictionaryDatasCompleted += new EventHandler<GetDictionaryDatasCompletedEventArgs>(client_GetDictionaryDatasCompleted);
            client.GetDictionaryDatasAsync(this.DataDictId);
        }

        void client_GetDictionaryDatasCompleted(object sender, GetDictionaryDatasCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.IsEnabled = false;
            }
            else if (!e.Result.IsSuccess)
            {
                this.IsEnabled = false;
            }
            else
            {
                this.m_comboBox.ItemsSource = e.Result.Data;
                SetDefaultSelected();
            }
        }

        #region override
        public override void UpdateSource()
        {
            DictionaryData dictData = m_comboBox.SelectedItem as DictionaryData;
            if (dictData != null)
            {
                DataSource.Value = dictData.Id.ToString("D");
                DataSource.Text = dictData.Caption;
            }
            this.GetBindingExpression(PMFDataControl.DataSourceProperty).UpdateSource();
        }

        protected override void OnIsReadOnlyChanged(bool isReadOnly)
        {
            if (isReadOnly)
            {
                m_txtName.Visibility = System.Windows.Visibility.Visible;
                this.m_comboBox.Visibility = System.Windows.Visibility.Collapsed;

            }
            else
            {
                m_txtName.Visibility = System.Windows.Visibility.Collapsed;
                m_comboBox.Visibility = System.Windows.Visibility.Visible;
            }
        }

        protected override void OnDataSourceChanged(FieldInstance dataSource)
        {
            SetDefaultSelected();
        }
        #endregion

        public void SetDefaultSelected()
        {
            if (DataSource == null || string.IsNullOrEmpty(DataSource.Value))
            {
                return;
            }

            if (m_comboBox.ItemsSource != null)
            {
                foreach (var data in m_comboBox.ItemsSource)
                {
                    DictionaryData dData = data as DictionaryData;
                    if (dData.Id == new Guid(DataSource.Value))
                    {
                        m_comboBox.SelectedItem = dData;
                        m_txtName.Text = dData.Caption;
                        break;
                    }
                }
            }
        }
    }
}
